package com.amir.common.mvp.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.amir.common.R;
import com.amir.common.holder.CommonViewHolder;
import com.amir.common.utils.AppManager;
import com.amir.common.utils.MyProgressLoading;
import com.amir.common.mvp.presenter.IPresenter;
import com.amir.common.mvp.view.IView;
import com.amir.common.utils.ToastUtil;

import org.jetbrains.annotations.Nullable;


/**
 * 版本：3.0
 * data：2019/1/29
 * com.aiyaopai.yaopai.mvp.base
 */
public abstract class BasePopuWindow<P extends IPresenter, V extends IView> extends PopupWindow implements IView, PopupWindow.OnDismissListener {
    protected Context mContext;
    protected View conttentView;
    private MyProgressLoading mLoading;
    private P mPresenter;
    protected CommonViewHolder mHolder;


    public BasePopuWindow(Context context, View contentView, int width, int height) {
        super(contentView, width, height);
        this.mContext = context;
        this.conttentView = contentView;
        this.setFocusable(true);
        mPresenter = createPresenter();
        this.setOutsideTouchable(true);
        mHolder = new CommonViewHolder(contentView);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mLoading = new MyProgressLoading(mContext, R.style.DialogStyle);
        this.setOnDismissListener(this);
        initBindHolder(mHolder);
    }

    @Nullable
    @Override
    public Context getViewContext() {
        return mContext;
    }

    public BasePopuWindow(Context context, int layoutid) {
        super(context);
        this.mContext = context;
        mPresenter = createPresenter();
        View contentView = LayoutInflater.from(context).inflate(layoutid, null);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setContentView(contentView);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        mHolder = new CommonViewHolder(contentView);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mLoading = new MyProgressLoading(mContext, R.style.DialogStyle);
        this.setOnDismissListener(this);
        initBindHolder(mHolder);
    }



    protected abstract void initBindHolder(CommonViewHolder holder);


    public BasePopuWindow(Context context) {
        super(context);
        mPresenter = createPresenter();
        this.mContext = context;
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x77000000));
        mLoading = new MyProgressLoading(mContext, R.style.DialogStyle);
        this.setOnDismissListener(this);
    }

    @Override
    public void showLoading() {
        mLoading.show();
    }

    @Override
    public void dismissLoading() {
        mLoading.dismiss();
    }

    @Override
    public void onError(@Nullable String mess) {
        Message message = new Message();
        message.obj = mess;
        message.what = 0;
        mHandler.sendMessage(message);
    }

    @Override
    public void showDialog(@Nullable String mess, int code) {

    }


    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);

        setBackgroundAlpha(0.7f);
    }

    @Override
    public void onAgainLogin() {
        mHandler.sendEmptyMessage(1);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {

                ToastUtil.Companion.show((String) msg.obj);
            } else {
                AppManager.getAppManager().finishAllActivity();
//                mContext.startActivity(new Intent(mContext,LoginActivity.class));
            }
        }
    };

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */

    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    @Override
    public void onDismiss() {
        setBackgroundAlpha(1f);
    }

    public P getPresenter() {


        return mPresenter;
    }

    abstract public P createPresenter();

    public V getMvpView() {

        return (V) this;
    }
}
